package com.hd.sup.config.mybatis;

import com.hd.sup.core.annotation.DataScope;
import org.apache.ibatis.mapping.MappedStatement;

import java.lang.reflect.Method;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title DataScopeUtils.java
 * @Description 数据权限工具类
 * @Package com.hd.sup.config.mybatis
 * @date 2021/1/25 9:05 上午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
public class DataScopeUtils {

    /**
     * @return com.hd.sup.config.mybatis.aop.DataScope
     * @author chentl
     * @description 根据 StatementHandler 获取 注解对象
     * @date 2021/1/25 9:05 上午
     * @params [mappedStatement]
     */
    public static DataScope getDataScopeByDelegate(MappedStatement mappedStatement) {
        DataScope DataScope = null;
        try {
            String id = mappedStatement.getId();
            String className = id.substring(0, id.lastIndexOf("."));
            String methodName = id.substring(id.lastIndexOf(".") + 1);
            final Class cls = Class.forName(className);
            final Method[] method = cls.getMethods();
            for (Method me : method) {
                if (me.getName().equals(methodName) && me.isAnnotationPresent(DataScope.class)) {
                    DataScope = me.getAnnotation(DataScope.class);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return DataScope;
    }
}
